package mian.java.concurrency.thread.synchronizedExample;

public class SynchronizedExample {

    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " acquired the lock.");
                try {
                    // 模拟长时间运行的任务
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-1");

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " acquired the lock.");
            }
        }, "Thread-2");

        thread1.start();
        thread2.start();
    }
}

